package com.evan.review.handler;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.evan.review.R;

/**
 * Handler机制学习主页面
 * <p>
 * 用于展示Handler的各种实现示例和功能
 */
public class HandlerMainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_handler_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initViews();
    }

    private void initViews() {
        // 初始化各个Handler示例按钮
        Button btnBasicHandler = findViewById(R.id.btn_basic_handler);
        Button btnHandlerThread = findViewById(R.id.btn_handler_thread);
        Button btnIdleHandler = findViewById(R.id.btn_idle_handler);
        Button btnSyncBarrier = findViewById(R.id.btn_sync_barrier);
        Button btnMemoryLeak = findViewById(R.id.btn_handler_memory_leak);

        btnBasicHandler.setOnClickListener(this);
        btnHandlerThread.setOnClickListener(this);
        btnIdleHandler.setOnClickListener(this);
        btnSyncBarrier.setOnClickListener(this);
        btnMemoryLeak.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_basic_handler) {
            // 跳转到基础Handler使用
            startActivity(new Intent(this, BasicHandlerActivity.class));
        } else if (id == R.id.btn_handler_thread) {
            // 跳转到HandlerThread示例
            startActivity(new Intent(this, HandlerThreadActivity.class));
        } else if (id == R.id.btn_idle_handler) {
            // 跳转到IdleHandler示例
            startActivity(new Intent(this, IdleHandlerActivity.class));
        } else if (id == R.id.btn_sync_barrier) {
            // 跳转到同步屏障示例
            startActivity(new Intent(this, SyncBarrierActivity.class));
        } else if (id == R.id.btn_handler_memory_leak) {
            // 跳转到Handler内存泄漏示例
            startActivity(new Intent(this, HandlerMemoryLeakActivity.class));
        }
    }
} 